package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Transactional(readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> Mapper;

    @Override
    @Transactional
    public void save(T t) {
        Mapper.save(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        Mapper.delete(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        Mapper.update(t);
    }

    @Override
    public T selectById(Long id) {
        return (T) Mapper.selectById(id);
    }

    @Override
    public List<T> selectAll() {
        return Mapper.selectAll();
    }
    //高级查询和分页
    @Override
    public PageBean<T> queryPage(BaseQuery baseQuery) {
        Long conut = Mapper.getConut(baseQuery);
        if (conut==0||conut<=0){
            return new PageBean<>();
        }else {
            List<T> list = Mapper.queryPage(baseQuery);
            return new PageBean<T>(conut,list);
        }
    }

    @Override
    @Transactional
    public void batchRemove(List<T> bases) {

        Mapper.batchRemove(bases);
    }

}
